#!/usr/bin/python
# -*- coding:UTF-8 -*-
# filename:blog.py

import web
import time

urls = (
    '/count_down/(.*)', 'count_down',
    '/count_holder', 'count_holder',
    '/(.*)', 'index',
    #'(.*)', 'reblog'
)

app_blog = web.application(urls, globals())

class reblog:
    def GET(self, path):
        #raise web.seeother('/')
        return 'Welcome to new.i-david.org, URI is /blog{0}, path is {0}'.format(path)

class index:
    def GET(self, path):
        #1/0   #测试404 500页面的定制
        return 'Welcome to new.i-david.org, URI is /blog/{0}, path is {0}'.format(path)

#钩子程序，加钩子
def my_loadhook():
    print 'my load hook'
    
#钩子程序，解钩子
def my_unloadhook():
    print 'my unload hook'

#添加钩子
app_blog.add_processor(web.loadhook(my_loadhook))
app_blog.add_processor(web.loadhook(my_unloadhook))

def notfound():
    return web.notfound('Sorry, the pate you were looking for was not found.')

def internalerror():
    return web.internalerror('Bad, bad server. No donut for you.')

app_blog.notfound = notfound
app_blog.internalerror = internalerror

class count_down:
    def GET(self, count):
        # These headers make it work in browsers
        web.header('Content-type', 'text/html')
        web.header('Transfer-Encoding', 'chunked')        
        yield '<h2>Prepare for Launch!</h2>'
        j = '<li>Liftoff in %s...</li>'
        yield '<ul>'
        count = int(count)
        for i in range(count,0,-1):
            out = j % i
            time.sleep(1)
            yield out
        yield '</ul>'
        time.sleep(1)
        yield '<h1>Lift off</h1>'

class count_holder:
    def GET(self):
        web.header('Content-type','text/html')
        web.header('Transfer-Encoding','chunked')        
        boxes = 4
        delay = 3
        countdown = 10
        for i in range(boxes):
            output = '<iframe src="/%d" width="200" height="500"></iframe>' % (countdown - i)
            yield output
            time.sleep(delay)
            
            